package com.hakim.wfb.customer.service.impl;

import com.hakim.wfb.customer.params.order.ReceiveAddressParameter;
import com.hakim.wfb.customer.result.order.ReceiveAddressView;
import com.hakim.wfb.customer.service.ReceiveAddressService;
import com.hakim.wfb.customer.service.convert.goods.ReceiveAdressDtv;
import com.hakim.wfb.dao.ReceiveAddressDao;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hakim.wfb.common.serial.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by lenovo on 2017/7/15.
 */
@Service("receiveAddressService")
public class ReceiveAddressServiceImpl implements ReceiveAddressService {

    @Autowired
    ReceiveAddressDao receiveAddressDao;

    @Override
    public ListResultEx<ReceiveAddressView> list(ReceiveAddressParameter parameter) {
        ListResultEx<ReceiveAddressView> res = new ListResultEx<ReceiveAddressView>();
        try {
            List<ReceiveAddressView> reList = receiveAddressDao.list(parameter);
            res.setTotalCount(reList.size());
            res.setDataList(reList);
            return res.makeSuccessResult();
        } catch (Exception e) {
            return res.makeFailedResult("查询失败" + e.getMessage());
        }
    }

    @Override
    public ResultEx addAddr(ReceiveAddressParameter addr) {
        ResultEx re = new ResultEx();
        try {
            ReceiveAddressParameter parameter = new ReceiveAddressParameter();
            parameter.setRaUser(addr.getRaUser());
            List<ReceiveAddressView> reList = receiveAddressDao.list(parameter);
            if (reList.size() >= 5)
                return re.makeFailedResult("最多只可以维护5个地址");

            addr.setTid(UUIDGenerator.getUUID());
            receiveAddressDao.add(ReceiveAdressDtv.parameter2Obj(addr));
            return re.makeSuccessResult();
        } catch (Exception e) {
            return re.makeFailedResult("操作失败" + e.getMessage());
        }
    }

    @Override
    public ResultEx delAddr(ReceiveAddressParameter addr) {
        ResultEx re = new ResultEx();
        try {
            receiveAddressDao.deleteByTid(addr.getTid());
            return re.makeSuccessResult();
        } catch (Exception e) {
            return re.makeFailedResult("操作失败" + e.getMessage());
        }
    }
}
